﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MouseKeyboardLibrary;
using System.Threading;
using System.IO;

namespace War3HostEnterer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private KeyboardHook kh = new KeyboardHook();
        private bool isStop = false;
        int _refreshInterval;
        private Point hostPoint;
        private int y;

        private void btn_Click(object sender, EventArgs e)
        {
            
        }

        void kh_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Z:
                    //开始
                    ThreadPool.QueueUserWorkItem(Fuck);
                    break;

                default:
                    break;
            }
        }

        void Fuck(object o)
        {
            while (!isStop)
            {
                Bitmap b = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                Graphics g = Graphics.FromImage(b);
                g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
                pictureBox1.Image = null;
                pictureBox1.Image = b;
                if (HasNewHost(b))//有主机
                {
                    //File.AppendAllText("state.txt", "双击了");
                    //加入
                    MouseSimulator.Position = hostPoint;
                    MouseSimulator.Click(MouseButton.Left);
                  //  Thread.Sleep(100);
                    KeyboardSimulator.KeyPress(Keys.Enter);
                    isStop = true;
                }

                Thread.Sleep(_refreshInterval);
            }
        }

        private bool HasNewHost(Bitmap b)
        {
            List<Color> cs = new List<Color>();
            for (int i = hostPoint.X; i < hostPoint.X + 20; i++)
            {
                var c = b.GetPixel(i, hostPoint.Y);
                cs.Add(c);
                if (c.R >= 200 && c.G >= 200 && c.B >= 200)
                {
                    return true;
                }


            }
            //foreach (var color in cs)
            //{
            //    File.AppendAllText(@"dd.txt", color.ToString());
            //}
            return false;
        }

        //void kh_KeyUp(object sender, KeyEventArgs e)
        //{
        //    MessageBox.Show(e.KeyCode.ToString());
        //}

        private void btnbutton1_Click(object sender, EventArgs e)
        {
            kh.Stop();
            isStop = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isStop = true;
            if (kh.IsStarted)
            {
                kh.Stop();
            }
        }

        private void txttextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                _refreshInterval = int.Parse(txttextBox1.Text);
                y = int.Parse(textBox1.Text);
                hostPoint = new Point(int.Parse(txttextBox2.Text), y);
                kh.KeyDown += new KeyEventHandler(kh_KeyDown);
                kh.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错");
            }
        }

        private void btnbutton2_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(Fuck);
        }
    }
}
